package com.scsj.supermarket.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.library.flowlayout.FlowLayoutManager;
import com.library.flowlayout.SpaceItemDecoration;
import com.scsj.supermarket.R;
import com.vondear.rxtools.RxDeviceTool;
import com.vondear.rxtools.RxImageTool;

import java.util.ArrayList;
import java.util.List;

/**
 * 规格
 */

public class SpecTypeAdapter extends RecyclerView.Adapter<SpecTypeAdapter.MyViewHolder> {

    private LayoutInflater minflater;
    private Context mContext;
    protected List<String> specTypeList = new ArrayList<>();
    protected List<String> specList = new ArrayList<>();

    public interface OnItemClickListener {
//        void onItemClick(View view, String data, int postion);

    }

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    private OnItemClickListener mOnItemClickListener;

    public SpecTypeAdapter(Context context) {
        this.mContext = context;
        minflater = LayoutInflater.from(context);
    }

    public void addAllData(List<String> specTypeList, List<String> specList) {
        this.specTypeList.addAll(specTypeList);
        this.specList.addAll(specList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.specTypeList.clear();
        this.specList.clear();
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = minflater.inflate(R.layout.item_spec_type, viewGroup, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;

    }

    public void add(int pos) {
//        mDatas.add(pos, "insert to");
        notifyItemChanged(pos);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder myViewHolder, final int i) {
        String specBean = specTypeList.get(i);
        myViewHolder.title.setText(specBean);

        final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) myViewHolder.spec_item_list.getLayoutParams();
        lp.width = RxDeviceTool.getScreenWidths(mContext);
        final FlowLayoutManager flowLayoutManager = new FlowLayoutManager();
        myViewHolder.spec_item_list.addItemDecoration(new SpaceItemDecoration(RxImageTool.dp2px(10)));
        myViewHolder.spec_item_list.setLayoutManager(flowLayoutManager);
        final SpecItemAdapter specItemAdapter = new SpecItemAdapter(mContext);
        myViewHolder.spec_item_list.setAdapter(specItemAdapter);
        myViewHolder.spec_item_list.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                myViewHolder.spec_item_list.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                lp.height = flowLayoutManager.getTotalHeight() +  myViewHolder.spec_item_list.getPaddingBottom() +  myViewHolder.spec_item_list.getPaddingTop();
                myViewHolder.spec_item_list.setLayoutParams(lp);
            }
        });
        specItemAdapter.addAllData(specList);
    }

    @Override
    public int getItemCount() {
        if (specTypeList != null) {
            return specTypeList.size();
        } else {
            return 0;
        }
    }

    protected void setItemEventClick(final MyViewHolder myViewHolder) {
        if (mOnItemClickListener != null) {
            myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int layoutPostion = myViewHolder.getLayoutPosition();
//                    SpecBean data = specTypeList.get(layoutPostion - 1);
//                    mOnItemClickListener.onItemClick(view,data, layoutPostion);
                }
            });
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView title;
        RecyclerView spec_item_list;

        public MyViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            spec_item_list = itemView.findViewById(R.id.spec_item_list);
        }
    }

}
